using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace TripleMatch
{
    public partial class DecorationsManager : MonoBehaviour
    {
        /// <summary>
        /// 装饰物配置信息
        /// </summary>
        private DecorationsData m_decorationInfoData;
        private DecoractionSaveData m_decoractionSaveData;
        private Dictionary<int, List<DecorationInUseData>> m_decorationInUse;

        private void InitDecorationsData()
        {
            //初始化配置信息
            // m_decorationInfoData = ResManager.S.LoadAsset<DecorationsData>("configDatas", "decorationsInfo");
            m_decorationInfoData = GameDefine.S.decorationDatas;

            //初始化储存信息
            string saveData = PlayerPrefs.GetString(GameDefine.SaveKey_DecorationData, string.Empty);
            if (string.IsNullOrEmpty(saveData)) m_decoractionSaveData = new DecoractionSaveData();
            else
            {
                m_decoractionSaveData = JsonUtility.FromJson<DecoractionSaveData>(saveData);
                if (m_decoractionSaveData == null) m_decoractionSaveData = new DecoractionSaveData();
            }

            for (int i = 0; i < m_decorationInfoData.infoDatas.Length; i++)
            {
                m_decorationInfoData.infoDatas[i].piecesTransRate =
                    GameDefine.S.Data_QualityTransRate[m_decorationInfoData.infoDatas[i].qualityIndex]
                    + GameDefine.S.Data_TypeTransRate[m_decorationInfoData.infoDatas[i].decorationType];
            }

            //初始化正在使用的装饰物信息
            m_decorationInUse = new Dictionary<int, List<DecorationInUseData>>();
            DecorationInUseData decorationInUseData = null;
            for (int i = 0; i < m_decoractionSaveData.decorationInUseDatas.Count; i++)
            {
                decorationInUseData = m_decoractionSaveData.decorationInUseDatas[i];
                if (!m_decorationInUse.ContainsKey(decorationInUseData.id))
                    m_decorationInUse.Add(decorationInUseData.id, new List<DecorationInUseData>());

                m_decorationInUse[decorationInUseData.id].Add(decorationInUseData);
                m_decorationInUse[decorationInUseData.id].Last().index = m_decorationInUse[decorationInUseData.id].Count;
                m_decorationInUse[decorationInUseData.id].Last().mixId = decorationInUseData.id * 10000 + m_decorationInUse[decorationInUseData.id].Count;
            }
            m_decoractionSaveData.decorationInUseDatas.Clear();
        }

        private void SaveDecorationsData()
        {
            if (m_decoractionSaveData != null)
            {
                foreach (var item in m_decorationInUse)
                {
                    for (int i = 0; i < item.Value.Count; i++)
                    {
                        m_decoractionSaveData.decorationInUseDatas.Add(item.Value[i]);
                    }
                }

                string saveData = JsonUtility.ToJson(m_decoractionSaveData);
                PlayerPrefs.SetString(GameDefine.SaveKey_DecorationData, saveData);
            }
        }

        private DecorationInfoData[] m_infoData;
        public DecorationInfoData[] GetDecorationInfoDatas()
        {
            if (m_infoData == null) m_infoData = m_decorationInfoData.infoDatas;
            return m_infoData;
        }

        /// <summary>
        /// 获取装饰物信息
        /// </summary>
        /// <param name="decorationId"></param>
        /// <returns></returns>
        public DecorationInfoData GetDecorationInfoData(int decorationId)
        {
            for (int i = 0; i < m_decorationInfoData.infoDatas.Length; i++)
            {
                if (m_decorationInfoData.infoDatas[i].id == decorationId) return m_decorationInfoData.infoDatas[i];
            }

            return null;
        }

        /// <summary>
        /// 获取装饰物组别信息
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public StyleGroupData GetStyleGroupData(int groupId)
        {
            for (int i = 0; i < m_decorationInfoData.groupDatas.Length; i++)
            {
                if (m_decorationInfoData.groupDatas[i].id == groupId) return m_decorationInfoData.groupDatas[i];
            }

            return null;
        }

        public void TransOnce(int decorationId)
        {
            DecorationInfoData decorationInfoData = GetDecorationInfoData(decorationId);

            for (int i = 0; i < m_decoractionSaveData.decorationPiecesDatas.Count; i++)
            {
                if (m_decoractionSaveData.decorationPiecesDatas[i].id == decorationId)
                {
                    m_decoractionSaveData.decorationPiecesDatas[i].pieseCount -= decorationInfoData.piecesTransRate;
                } 
            }

            AddDecorationData(decorationId, 1);
        }

        /// <summary>
        /// 获取当前碎片信息
        /// </summary>
        /// <param name="decorationId"></param>
        /// <returns></returns>
        public DecorationPiecesData GetDecorationPiecesData(int decorationId)
        {
            for (int i = 0; i < m_decoractionSaveData.decorationPiecesDatas.Count; i++)
            {
                if (m_decoractionSaveData.decorationPiecesDatas[i].id == decorationId) return m_decoractionSaveData.decorationPiecesDatas[i];
            }

            return null;
        }

        /// <summary>
        /// 添加玩家碎片信息
        /// </summary>
        /// <param name="decorationId"></param>
        /// <param name="piecesCount"></param>
        /// <returns>如果满足兑换条件，且为首次兑换，返回true</returns>
        public bool AddDecorationPicesData(int decorationId, int piecesCount)
        {
            bool contanedData = false;
            DecorationPiecesData decorationPiecesData = null;
            for (int i = 0; i < m_decoractionSaveData.decorationPiecesDatas.Count; i++)
            {
                if (m_decoractionSaveData.decorationPiecesDatas[i].id == decorationId)
                {
                    decorationPiecesData = m_decoractionSaveData.decorationPiecesDatas[i];
                    m_decoractionSaveData.decorationPiecesDatas[i].pieseCount += piecesCount;
                    contanedData = true;
                    break;
                } 
            }

            if (!contanedData)
            {
                decorationPiecesData = new DecorationPiecesData();
                decorationPiecesData.id = decorationId;
                decorationPiecesData.pieseCount = piecesCount;
                m_decoractionSaveData.decorationPiecesDatas.Add(decorationPiecesData);
            }

            DecorationInfoData decorationInfoData = GetDecorationInfoData(decorationId);
            if (decorationInfoData != null && decorationPiecesData.pieseCount >= decorationInfoData.piecesTransRate && GetDecorationData(decorationId)==null)
            {
                decorationPiecesData.pieseCount -= decorationInfoData.piecesTransRate;
                AddDecorationData(decorationId, 1);
                return true;
            }

            return false;
        }

        /// <summary>
        /// 获取当前装饰物信息
        /// </summary>
        /// <param name="decorationId"></param>
        /// <returns></returns>
        public DecorationData GetDecorationData(int decorationId)
        {
            for (int i = 0; i < m_decoractionSaveData.decorationDatas.Count; i++)
            {
                if (m_decoractionSaveData.decorationDatas[i].id == decorationId) return m_decoractionSaveData.decorationDatas[i];
            }

            return null;
        }

        public List<DecorationData> GetDecorationDatas()
        {
            return m_decoractionSaveData.decorationDatas;
        }

        /// <summary>
        /// 添加当前装饰物信息
        /// </summary>
        /// <param name="decorationId"></param>
        /// <returns></returns>
        public void AddDecorationData(int decorationId, int count)
        {
            bool contanedData = false;
            for (int i = 0; i < m_decoractionSaveData.decorationDatas.Count; i++)
            {
                if (m_decoractionSaveData.decorationDatas[i].id == decorationId)
                {
                    m_decoractionSaveData.decorationDatas[i].ownCount += count;
                    contanedData = true;
                }
            }

            if (!contanedData)
            {
                DecorationData decorationData = new DecorationData();
                decorationData.id = decorationId;
                decorationData.ownCount = count;
                m_decoractionSaveData.decorationDatas.Add(decorationData);
            }
        }

        //public void SetDecorationInUseData(int mixId, int layer, Vector2 pos)
        //{ 
        //    int decorationId = mixId / 10000;
        //    int index = mixId % 10000;

        //    m_decorationInUse[decorationId][index - 1].layer = layer;
        //    m_decorationInUse[decorationId][index - 1].pos = pos;
        //}

        public Dictionary< int,List<DecorationInUseData>> GetDecorationsInUse()
        {
            return m_decorationInUse;
        }

        //public void AddDecorationInUseData(int decorationId, int layer, Vector2 pos)
        //{
        //    DecorationInUseData decorationInUseData = new DecorationInUseData();
        //    decorationInUseData.id = decorationId;
        //    decorationInUseData.layer = layer;
        //    decorationInUseData.pos = pos;

        //    if (!m_decorationInUse.ContainsKey(decorationId)) m_decorationInUse.Add(decorationId, new List<DecorationInUseData>());
        //    if (m_decorationInUse[decorationId].Count == 0)
        //    {
        //        decorationInUseData.index = 1;
        //        decorationInUseData.mixId = decorationId * 10000 + decorationInUseData.index;
        //        m_decorationInUse[decorationId].Add(decorationInUseData);
        //    }
        //    else
        //    {
        //        DecorationInUseData decorationInUseDataLast = m_decorationInUse[decorationId].Last();
        //        decorationInUseData.index = decorationInUseDataLast.index + 1;
        //        decorationInUseData.mixId = decorationId * 10000 + decorationInUseData.index;
        //        m_decorationInUse[decorationId].Add(decorationInUseData);
        //    }
        //}

        //public void RemoveDecorationInUseData(int mixId)
        //{
        //    int decorationId = mixId / 10000;
        //    int index = mixId % 10000;

        //    if (m_decorationInUse.ContainsKey(decorationId) && m_decorationInUse[decorationId].Count >= index)
        //        m_decorationInUse[decorationId].RemoveAt(index - 1);
        //}
    }
}

